<?php
namespace app\admin\validate;

/**
 * 验证器
 */
use think\Validate;

class Arctype extends Validate
{
    protected $rule = [
        'title' => 'require|max:25|unique:arctype',
        'pid'   => 'number',
    ];

    protected $message = [
        'title.require' => '名称必须',
        'title.max'     => '名称最多不能超过25个字符',

        'pid.number'    => '上级栏目参数错误',
        'pid.checkpid'  => '上级栏目不能为自身',

    ];

    /* 止写法已经不支持了
    protected $scene = [
    'edit'  =>  [
    'title'     =>  'require|max:25|unique:arctype',
    'pid'       =>  'number|checkpid:pid',
    ],
    ];

    protected $scene = [
    'edit'  =>  ['title'=>'require|max:25|unique:arctype'],
    ];

     */
    // edit 验证场景定义
    public function sceneEdit()
    {
        return $this->only(['title', 'pid'])
            ->remove('title', 'unique')
            ->append('title', 'unique:arctype')
            ->append('pid', 'checkpid:pid');

    }

    // 自定义验证规则
    protected function checkpid($value, $rule, $data = [])
    {
        if ($data['id'] === $data['pid']) {
            return false;
        } else {
            return true;
        }

    }

/*
// edit 验证场景定义
public function sceneedit()
{
return $this->only(['username','password','grade'])
->remove('password', 'require');
}
 */
/*
// 自定义验证规则
protected function checkName($value,$rule,$data=[])
{
if(empty($data['id'])){
$where = ['username'=>$value]; //新增加时
}else{
$where = ['username'=>$value,'id'=>['neq',$data['id']]];  //修改时
}
$typename = model('user')->where($where)->select();
if(empty($typename)) {
return true;
}else{
return false;
}
}
 */
}
